package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Rules2;
import com.example.demo.entity.User;
import com.example.demo.mapper.Rules2Mapper;
import com.example.demo.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController

@RequestMapping(value ={"/student","/user"})
@CrossOrigin

public class Rules2Controller {
    @Resource
    UserMapper userMapper;
    @Resource
    Rules2Mapper rules2Mapper;

    @PostMapping("/g1")
    public Map<String, String> saveselect(@RequestBody String info) {
        System.out.println(info);
        JSONObject sjsoninfo = JSONObject.parseObject(info);//取出前端的JSON
        String sgrade = "研二";
        QueryWrapper<Rules2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("grade", sgrade);
        Rules2 yearrule = rules2Mapper.selectOne(queryWrapper);
        String rules = yearrule.getRule();
        JSONObject rulejson = JSONObject.parseObject(rules); //规则表里的JSON

        JSONObject coursejson = (JSONObject) rulejson.get("学业成绩"); //把学业成绩取出来
        double courseweight = Double.parseDouble(coursejson.getString("学位成绩权重")); //取出学位成绩权重的值
        int courseScore = sjsoninfo.getIntValue("成绩");
        int courseNum = sjsoninfo.getIntValue("数量");
        if (courseNum == 0) {
            // 判断学位课数量是否符合
            Map<String, String> errorResponse = new HashMap<>();
            errorResponse.put("错误信息", "学位课数量不能为0!");
            return errorResponse;
        }
        double stotal = 20;
        stotal += courseScore / courseNum * courseweight;
         /*
        思政表现板块
         */
        JSONObject policyrule = (JSONObject) (rulejson.get("思政成绩")); //取出规则里的思政成绩
//
        String policyscoreStr = sjsoninfo.getString("思政成绩");   //取出信息里的思政成绩
        JSONObject policyscore = JSONObject.parseObject(policyscoreStr);//转换取出的思政成绩
        //荣誉分
        JSONObject honorvalue = (JSONObject) (policyrule.get("荣誉分")); //取出rule表对应的规则
        int honorscore1 = policyscore.getIntValue("获得省级奖项次数");
        System.out.println(honorscore1);
        int honorscore2 = policyscore.getIntValue("获得市级奖项次数");
        System.out.println(honorscore2);
        int honorscore3 = policyscore.getIntValue("获得校级校级次数");
        int honorscore = honorscore1*Integer.parseInt(honorvalue.get("省级奖项").toString())+honorscore2*Integer.parseInt(honorvalue.get("市级奖项").toString())+honorscore3*Integer.parseInt(honorvalue.get("校级奖项").toString());


        if(honorscore>5){ honorscore=5; }//荣誉分不能超过5


        //导师组评价

        JSONObject teachervalue = (JSONObject) (policyrule.get("教师评价分"));
        System.out.println(teachervalue);


        int teacherscore = Integer.parseInt(teachervalue.get(policyscore.get("教师评价分")).toString());

        //基础分

        int pervalue = Integer.parseInt(policyrule.get("通报批评扣分数").toString());
        int Notificationtimes = sjsoninfo.getIntValue("通报次数");
        int Notificationscore = pervalue * Notificationtimes;

        int basicscore = 20;
        basicscore -= Notificationscore; //通报扣分总值
        if(basicscore < 0){ basicscore = 0; }//基础分不能小于0
        int sizhengscore = basicscore + honorscore +teacherscore;
        if(sizhengscore > 30) { sizhengscore = 30; }
        stotal += sizhengscore;
        //学术论文
        JSONObject paperrule = (JSONObject) (rulejson.get("学术论文")); //取出规则里的学术论文
        String paperscoreStr = sjsoninfo.getString("学术论文");   //取出信息里的学术论文
        JSONObject paperscore = JSONObject.parseObject(paperscoreStr);//取出信息里的学术论文
        int  paperscore1 = paperscore.getIntValue("一类次数");
        int  paperscore2 = paperscore.getIntValue("一类次数");
        int  paperscore3 = paperscore.getIntValue("一类次数");
        int  paperscoresum =  paperscore1*Integer.parseInt(paperrule.get("1").toString())+ paperscore2*Integer.parseInt(paperrule.get("2").toString())+ paperscore3*Integer.parseInt(paperrule.get("3").toString());
        if(paperscoresum>30){paperscoresum=30;}
        stotal +=paperscoresum;


        String cover = sjsoninfo.getString("附件");

        String sno = sjsoninfo.getString("学号");
        int studentNumber = Integer.parseInt(sno);

        User user= userMapper.selectOne(new QueryWrapper<User>().eq("sno", studentNumber));
        if (user != null) {
            user.setStotal(stotal);
            user.setCover(cover);
            userMapper.updateById(user);
        }
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("申请状态", "success");

        return resultMap;


    }
}
